<?php
namespace Admin\Controller;

class LoginController extends BaseController{
    public function index(){
    	if (IS_POST){
    		$Manager = D('Manager');
    		$object = $Manager->login();
    		if (is_array($object)){
    			if (sha1(I('post.pass')) != $object['pass']) $this->failed('管理员帐号或密码不正确！');
				if (empty($object['level'])) $this->failed('您的帐号尚未激活，无法登录，请联系超级管理员进行激活！');
				session(C('SESSION_KEY'),array(
					'id'=>$object['id'],
					'name'=>$object['name'],
					'level'=>$object['level'],
					'permit'=>$object['permit'],
					'order_permit'=>$object['order_permit']
				));
				$this->success(U('Index/index'));
    		}else{
    			$this->failed($object);
    		}
    	}
    	$this->display();
    }
	
	public function ajax(){
    	if (IS_AJAX){
    		$Manager = D('Manager');
    		$object = $Manager->login();
			if (is_array($object)){
				if (sha1(I('post.pass'))==$object['pass']){
					echo empty($object['level']) ? 0 : 1;
				}else{
 					echo 2;
				}
			}else{
				echo 2;
			}
    	}
	}
	
	public function qq(){
		include ROOT_PATH.'/ThinkPHP/Library/Yvjie/qq/comm/utils.php';
		include ROOT_PATH.'/ThinkPHP/Library/Yvjie/qq/class/QC.class.php';
		$qc = new \QC('','',C('WEB_URL').'Callback/qqAdmin.php');
		$qc->qq_login();
	}
	
	public function qqReturn(){
		if (I('get.skip') == '1'){
			if (C('QQ_VERIFY') == '1'){
				session(C('SESSION_KEY'),array(
					'id'=>0,
					'name'=>session(C('SESSION_KEY').'nickname'),
					'level'=>1
				));
			}
			$this->success(C('WEB_URL').C('MANAGE_ENTER'));
		}
		$Manager = D('Manager');
		if (IS_POST){
			$object = $Manager->login();
			if (is_array($object)){
				if (sha1(I('post.pass')) != $object['pass']) $this->failed('管理员帐号或密码不正确！');
				if (!empty($object['qqau'])) $this->failed('此帐号已绑定了其它QQ，无法再次绑定，如需改绑，请联系超级管理员进行解绑！');
				if ($Manager->qq(I('post.qqau'),$object['id'])){
					session(C('SESSION_KEY'),array(
						'id'=>$object['id'],
						'name'=>$object['name'],
						'level'=>$object['level'],
						'permit'=>$object['permit'],
						'order_permit'=>$object['order_permit']
					));
					$this->success(C('WEB_URL').C('MANAGE_ENTER'),'QQ绑定成功，即将跳转到后台！');
				}else{
					$this->failed('QQ绑定失败，请重试！');
				}
			}else{
				$this->failed($object);
			}
		}
		include ROOT_PATH.'/ThinkPHP/Library/Yvjie/qq/comm/utils.php';
		include ROOT_PATH.'/ThinkPHP/Library/Yvjie/qq/class/QC.class.php';
		$qc = new \QC('','',C('WEB_URL').'Callback/qqAdmin.php');
		$callback = $qc->qq_callback();
		$openid = $qc->get_openid();
		$object = $Manager->qqlogin($openid);
		if ($object){
			if (empty($object['level'])) $this->failed('您的帐号尚未激活，无法登录，请联系超级管理员进行激活！');
			session(C('SESSION_KEY'),array(
				'id'=>$object['id'],
				'name'=>$object['name'],
				'level'=>$object['level'],
				'permit'=>$object['permit'],
				'order_permit'=>$object['order_permit']
			));
			$this->success(C('WEB_URL').C('MANAGE_ENTER'));
		}else{
			$qc2 = new \QC($callback,$openid);
			$info = $qc2->get_user_info();
			session(C('SESSION_KEY').'nickname',$info['nickname']);
			$this->assign('Nickname',$info['nickname']);
			$this->assign('Qqau',$openid);
			$this->display();
		}
	}
	
	public function qqAjax(){
    	if (IS_AJAX){
    		$Manager = D('Manager');
    		$object = $Manager->login();
			if (is_array($object)){
				if (sha1(I('post.pass'))==$object['pass']){
					echo 1;
				}else{
 					echo 2;
				}
			}else{
				echo 2;
			}
    	}
	}
	
	public function logout(){
		session(C('SESSION_KEY'),NULL);
		$this->success(U(CONTROLLER_NAME.'/index'));
	}
}